' Script to create MSCS resources if necessary and set all connection strings
' ================================================================================
' Vars used throughout script
' - This computer name
' - SQL computer name (defaults to this computer)
' - SQL user name and password (defaults to current user trusted connection)
' - MSCS site configuration object
Dim sThisComputer, sSQLComputer, sUserName, sPassword, oSiteConfig
sUserName = ""
sPassword = ""

' Create and initialize the SiteConfig to the default site
Dim sSiteName
' Change this value to point to your site
sSiteName = "Template"
Set oSiteConfig = CreateObject("Commerce.SiteConfig")
oSiteConfig.Initialize sSiteName

' Command line arguments (optional): SQL-computer-name db-user-name db-user-password
GetCommandLineArgs

' Make resource entries in MSCS database and via API
MakeMSCSDBEntries

' Clean up
Set oSiteConfig = Nothing

' ### End of main script

' ================================================================================
Sub GetCommandLineArgs
	Dim oWshNetwork, oArgs

	' Use WshNetwork object to get this computer name
	Set oWshNetwork = CreateObject("Wscript.Network")
	sThisComputer = oWshNetwork.ComputerName
	Set oWshNetwork = Nothing

	Set oArgs = WScript.Arguments
	If oArgs.Count > 0 Then
		sSQLComputer = oArgs(0)
		If oArgs.Count > 1 Then
			sUserName = oArgs(1)
			If oArgs.Count > 2 Then
				sPassword = oArgs(2)
			End If
		End If
	Else
		sSQLComputer = sThisComputer
	End If
	Set oArgs = Nothing

	'--WScript.Echo "This == " & sThisComputer
	'--WScript.Echo "SQL  == " & sSQLComputer
End Sub

' ================================================================================
Sub MakeMSCSDBEntries
	Dim ii, oADOadmin, sConnection

	' New database and resource names ********** match order
	Dim sNewDatabases(1)
	' modify this value to match your database
	sNewDatabases(0) = "Template"

	' New resource names ********** match order
	Dim sNewResourceNames(1)
	' modify this value to match your template resource
	sNewResourceNames(0) = "Template"

	Set oADOadmin = CreateObject("ADODB.Connection")
	sConnection = "Provider=SQLOLEDB;Data Source=" & sSQLComputer & ";Initial Catalog=MSCS_Admin;"
	If sUserName = "" Then
		sConnection = sConnection & "Integrated Security=SSPI;"
		oADOadmin.Open sConnection
	Else
		oADOadmin.Open sConnection, sUserName, sPassword
	End If

	For ii = 0 To 0
		CreateResource sNewDatabases(ii), sNewResourceNames(ii), oADOadmin
	Next
	
	oADOadmin.Close
	Set oADOadmin = Nothing
End Sub

' ================================================================================
' Create resource using MSCS site configuration object
Sub CreateResource(sName, sResourceName, oADOconn)
	Dim sConfigClass, sConnStrName, sDBCmd
	Dim sExpirationInterval, sExpirationInterval_Description, sExpirationInterval_Display, iExpirationInterval

	' MSCS DB stuff
	sConfigClass = "MSCS_" & sName
	sConnStrName = "connstr_db_" & sName

	sDBCmd = "USE MSCS_Admin" & vbCrLf
	sDBCmd = sDBCmd & "IF NOT EXISTS (SELECT s_ClassName FROM SystemProps WHERE s_ClassName = '" & sConfigClass
	sDBCmd = sDBCmd & "') INSERT INTO SystemProps (s_Name, s_ClassName, s_ResourceType, s_Description, f_ResourceFlags, f_PupFlags) "
	sDBCmd = sDBCmd & "VALUES ('', '" & sConfigClass & "', '" & sName & "', '" & sResourceName & "', 2, 1)" & vbCrLf

	sDBCmd = sDBCmd & "IF NOT EXISTS (SELECT s_PropertyName FROM ExtendedProps WHERE s_PropertyName = '" & sConnStrName
	sDBCmd = sDBCmd & "') INSERT INTO ExtendedProps VALUES ('" & sName & "', '" & sConnStrName & "', 1, 0, 0, '"
	sDBCmd = sDBCmd & sResourceName & "', 8, '', '" & sName & " Database')" & vbCrLf

	' Do DB commands
	'--WScript.Echo sDBCmd
	oADOconn.Execute sDBCmd

	' Resource stuff
	Dim str
	str = ""
	On Error Resume Next
	str = oSiteConfig.Fields(sResourceName).Value.Fields("s_Description")
	On Error GoTo 0

	' If Resource already exists, skip creation step
	If str = "" Then
		oSiteConfig.CreateComponentConfig sResourceName, sConfigClass
		oSiteConfig.Fields(sResourceName).Value.Fields("s_Description") = sResourceName
	End If

	' Save resource creation script as a pup script
	oSiteConfig.PutSQLScript sResourceName, sName, sDBCmd
	oSiteConfig.SaveConfig
End Sub